package com.jtjrenren.android.taxi.passenger.core.order.disptach;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.DialogInterface;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jtjrenren.android.taxi.passenger.core.order.status.OrderStatusActivity;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseMvpActivity;
import com.jtjrenren.android.taxi.passenger.bean.Order;
import com.jtjrenren.android.taxi.passenger.utils.UIHelper;
import com.library.weidget.CustomeDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wdl on 2016/1/22.
 * 订单未被接收之前的状态 比如分发、筛选
 * 必须将订单传递进来
 */
public class OrderPreStatusActivity extends BaseMvpActivity<PreStatusPresenter> implements PreStatusView {
    public static final String EXTRACT_ORDER = "order";  //从打车进来 接收通用回复
    public static final String EXTRACT_ORDERSERNO = "order_serno"; //从历史订单进来 已经有了电召单号

    @BindView(R.id.order_pre_dispatch)
    LinearLayout rl_dispatch;
    @BindView(R.id.order_dispatch_notifynum)
    TextView tv_notfiy;
    @BindView(R.id.order_dispatch_acceptnum)
    TextView tv_accept;

    @BindView(R.id.order_pre_filter)
    LinearLayout ll_filter;
    @BindView(R.id.order_filter_acceptnum)
    TextView tv_accept_f;
    @BindView(R.id.iv_anim_rote)
    ImageView imageView;
    @BindView(R.id.iv_cancle_order)
    ImageView iv_cancle;
    @BindView(R.id.tv_order_cancle)
    TextView tv_order_cancle;

    private ObjectAnimator rotation;
    float currentValue = 0;
    private CustomeDialog cancleDialog;
    private boolean isCancle = false;


    @Override
    public int getLayoutId() {
        return R.layout.atydlg_order_pre;
    }

    @Override
    public PreStatusPresenter createPresenter() {
        return new PreStatusPresenterImpl(this);
    }

    @Override
    public void initM() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.8f; //设置透明值
        lp.dimAmount = 0.8f; //设置黑暗度
        getWindow().setAttributes(lp);

        presenter.init();

        if (getIntent().hasExtra(EXTRACT_ORDER))
            presenter.initOrder((Order) getIntent().getSerializableExtra(EXTRACT_ORDER));

        if (getIntent().hasExtra(EXTRACT_ORDERSERNO))
            presenter.initOrderID(getIntent().getStringExtra(EXTRACT_ORDERSERNO));

    }


    @Override
    public void showOrderDispatchView() {
        clearViewStatus();

        rl_dispatch.setVisibility(View.VISIBLE);
        startScan();
    }

    @Override
    public void showOrderFilterView(int accepDriverNum) {
        clearViewStatus();
        ll_filter.setVisibility(View.VISIBLE);

        tv_accept_f.setText(accepDriverNum + "");
    }


    @Override
    public void refreshNotifCar(int count) {
        tv_notfiy.setText(count + "");
    }

    @Override
    public void refreshAcceptCar(int count) {
        tv_accept.setText(count + "");
    }

    @Override
    public void showWaitting(String desc) {
        showWaittingDialog(desc, true);
    }

    @Override
    public void dissWaitting() {
        dismissWaittingDialog();
    }

    @Override
    public void finshAty() {
        finish();
    }

    @Override
    public void startScan() {
        rotation = ObjectAnimator.ofFloat(imageView, "Rotation",
                currentValue - 360, currentValue);
        rotation.setDuration(1500);
        rotation.setRepeatCount(ObjectAnimator.INFINITE);
        rotation.setInterpolator(new LinearInterpolator());
        rotation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentValue = (Float) animation.getAnimatedValue();
            }
        });
        rotation.start();
    }

    @Override
    public void stopScan() {
        if (rotation != null)
            rotation.end();
        rotation = null;
    }


    @Override
    public void showTip(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void jdFinish(String orderID) {
        playDingDong();
        vibratorF();
        startActivity(OrderStatusActivity.getDiyIntent(this, orderID));
        finish();
    }

    private void clearViewStatus() {
        rl_dispatch.setVisibility(View.GONE);
        ll_filter.setVisibility(View.GONE);
        tv_order_cancle.setVisibility(View.GONE);
    }

    @OnClick(R.id.iv_cancle_order)
    public void clickCancle() {
        showCancleOrder();
    }

    private void showCancleOrder() {
        cancleDialog = UIHelper.getAlertDialog2(OrderPreStatusActivity.this, "您是否取消订单？", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                presenter.cancleOrder();
                clearViewStatus();
                tv_order_cancle.setVisibility(View.VISIBLE);
                iv_cancle.setVisibility(View.GONE);
//                stopScan();
                dialog.dismiss();
                isCancle = true;
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        cancleDialog.show();
    }


    @Override
    protected void onStop() {
        super.onStop();
        stopScan();
    }

    @Override
    protected void onPause() {
        super.onPause();
        dismissWaittingDialog();
    }

    @Override
    public void onBackPressed() {
        if (!isCancle)
            showCancleOrder();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopScan();
        if (cancleDialog != null && cancleDialog.isShowing())
            cancleDialog.cancel();
        cancleDialog = null;
        presenter.destory();
    }
}
